using System;
using System.IO;
using miPics.Common;
using miPics.Rss;


public partial class _Administration_DeleteGallery : miPics.BasePage.BasePage
{
	private bool ValidGallery = false;
	private short GalleryID;

	protected void Page_Load(object sender, EventArgs e)
	{
		// set menu administration task
		Master.SetMenuAdministrationTask("Delete gallery");

		// load image we are going to delete
		this.GalleryID = ToolKit.ReturnShort(Request.QueryString["id"]);

		// get gallery details into datatable through table adapter
		miPicsDataAccessTableAdapters.GalleryItemTableAdapter GalleryItemTableAdapter = new miPicsDataAccessTableAdapters.GalleryItemTableAdapter();
		miPicsDataAccess.GalleryItemDataTable GalleryItemDataTable = GalleryItemTableAdapter.GetData(this.GalleryID,this.IsLoggedIn);

		// setup interface if gallery was/was not found
		this.GalleryLoaded = (GalleryItemDataTable.Count == 1);

		// was a gallery record found?
		if (GalleryItemDataTable.Count == 1)
		{
			this.ValidGallery = true;

			// place gallery record into item row object
			miPicsDataAccess.GalleryItemRow GalleryItemRow = (miPicsDataAccess.GalleryItemRow)GalleryItemDataTable.Rows[0];

			// if not postback, populate fields with gallery data
			if (!Page.IsPostBack)
			{
				// pass gallery image details to control
				if (string.IsNullOrEmpty(GalleryItemRow.Thumbnail))
				{
					_GalleryImage.Src = Config.General.NoGalleryImageSrc;
					_GalleryImage.Width = Config.Image.WidthThumbnail;
					_GalleryImage.Height = Config.Image.HeightThumbnail;
					_GalleryImage.Alt = Config.General.NoGalleryImageName;
				}
				else
				{
					_GalleryImage.Src = "~/_Resource/Image/" + GalleryItemRow.Thumbnail;
					_GalleryImage.Width = GalleryItemRow.ThumbnailWidth;
					_GalleryImage.Height = GalleryItemRow.ThumbnailHeight;
					_GalleryImage.Alt = ToolKit.GetAltImageName(GalleryItemRow.ThumbnailName);
				}

				// pass gallery desc details
				_GalleryName.InnerText = GalleryItemRow.Name;
				_GalleryDescription.InnerText = GalleryItemRow.Description;

				// get gallery image count
				_GalleryImageCount.InnerText = GalleryItemTableAdapter.GetImageCount(this.GalleryID, true).ToString();
			}
		}
	}

	protected void DeleteGallery(object sender, EventArgs e)
	{
		if (this.ValidGallery)
		{
			// start by deleting all images in the gallery from the filesystem
			miPicsDataAccessTableAdapters.ImageListTableAdapter ImageListTableAdapter = new miPicsDataAccessTableAdapters.ImageListTableAdapter();
			miPicsDataAccess.ImageListDataTable ImageListDataTable = ImageListTableAdapter.GetData(this.GalleryID,this.IsLoggedIn);

			string ImageFolder = Request.PhysicalApplicationPath + "_Resource\\Image\\";

			// delete all images in the gallery
			foreach (miPicsDataAccess.ImageListRow ImageItem in ImageListDataTable)
			{
				// delete thumbnail image
				ToolKit.DeleteFile(ImageFolder + ImageItem.Thumbnail);
					
				// delete fullsize image
				ToolKit.DeleteFile(ImageFolder + ImageItem.Fullsize);
			}

			// now delete gallery database record and all database image records
			miPicsDataAccessTableAdapters.GalleryItemTableAdapter GalleryItemTableAdapter = new miPicsDataAccessTableAdapters.GalleryItemTableAdapter();
			GalleryItemTableAdapter.DeleteItem(this.GalleryID);

			// update gallery rss
			WriteRss GalleryRss = new WriteRss(this.Request);
			GalleryRss.SaveRss();

			// redirect back to the home page
			Response.Redirect("~/");
		}
	}

	protected void CancelDelete(object sender, EventArgs e)
	{
		// redirect back to the home page
		Response.Redirect("~/");
	}

	private bool GalleryLoaded
	{
		set
		{
			_ErrorMessage.Visible = !value;
			_Interface.Visible = value;
		}
	}
}